#include "muduo/proto/dispatcher.h"
#include "muduo/proto/codec.h"

#include "muduo/base/Logging.h"
#include "muduo/base/Mutex.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/TcpClient.h"
#include "muduo/net/EventLoopThread.h"
#include "muduo/base/CountDownLatch.h"

#include "request.pb.h"
#include <iostream>
#include <string>
#include <functional>

class Client
{
private:
    typedef std::shared_ptr<google::protobuf::Message> MessagePtr;
    typedef std::shared_ptr<bit::TranslateResponse> TranslateResponsePtr;
    typedef std::shared_ptr<bit::AddResponse> AddResponsePtr;

public:
    Client(const std::string &sip, int sport) : _latch(1), 
        _client(_loopthread.startLoop(), muduo::net::InetAddress(sip, sport), "Client"),
        _dispatcher(std::bind(&Client::OnUnkonwnMessage, this, 
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
        _codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher, 
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
    {
        _dispatcher.registerMessageCallback<bit::TranslateResponse>(std::bind(&Client::OnTranslate, this, 
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        _dispatcher.registerMessageCallback<bit::AddResponse>(std::bind(&Client::OnAdd, this, 
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));

        _client.setConnectionCallback(std::bind(&Client::OnConnection, this, std::placeholders::_1));
        _client.setMessageCallback(std::bind(&ProtobufCodec::onMessage, &_codec, 
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
    }

    void Connect()
    {
        _client.connect();
        _latch.wait();
    }

    void Translate(const std::string &message)
    {
        bit::TranslateRequest req;
        req.set_msg(message);
        Send(&req);
    }

    void Add(int num1, int num2)
    {
        bit::AddRequest req;
        req.set_num1(num1);
        req.set_num2(num2);
        Send(&req);
    }

private:
    bool Send(const google::protobuf::Message *message)
    {
        if(_conn->connected())
        {
            _codec.send(_conn, *message);
            return true;
        }
        return false;
    }
 
    void OnUnkonwnMessage(const muduo::net::TcpConnectionPtr &conn, const MessagePtr &message, muduo::Timestamp)
    {
        std::cout << "OnUnkonwnMessage: " << message->GetTypeName() << std::endl;
        _conn->shutdown();
    }

    void OnTranslate(const muduo::net::TcpConnectionPtr &conn, const TranslateResponsePtr &message, muduo::Timestamp)
    {
        std::cout << "翻译结果: " << message->msg() << std::endl;
    }

    void OnAdd(const muduo::net::TcpConnectionPtr &_conn, const AddResponsePtr &message, muduo::Timestamp)
    {
        std::cout << "相加结果: " << message->result() << std::endl;
    }

    void OnConnection(const muduo::net::TcpConnectionPtr &conn)
    {
        if(conn->connected())
        {
            _latch.countDown(); // 唤醒主线程的阻塞
            _conn = conn;
        }
        else _conn.reset(); // 连接关闭时操作
    }


private:
    muduo::CountDownLatch _latch; // 用于实现同步
    muduo::net::EventLoopThread _loopthread; // 异步循环处理线程
    muduo::net::TcpConnectionPtr _conn;
    muduo::net::TcpClient _client;
    ProtobufDispatcher _dispatcher;
    ProtobufCodec _codec;
};


int main()
{
    Client client("127.0.0.1", 8085);
    client.Connect();

    client.Translate("hello");
    client.Add(12, 12);

    sleep(1);

    return 0;
}